<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\InPressArticle;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use View;


use Illuminate\Support\Facades\Response;

use App\Lib\Export;


class InPressArticleController extends Controller
{
	public function list()
	{
		$articles = InPressArticle::simplePaginate(15);

		return view('in_press_article.list', ['articles' => $articles]);
	}

	public function index($id)
	{
		$article = InPressArticle::find($id);
		
		return view('in_press_article.index', ['article' => $article]);
	}

    public function downloadFile(Request $request, $id , $type) 
    {
        $article = InPressArticle::find($id);
        $file = $article->title . '.' . $type;
        if ($type == 'pdf') {
            $article->download_count = $article->download_count + 1;
            $article->save();

            if ($article['pdf'] == null ) {
                return redirect()->action('IndexController@index');
            }
            $name = public_path('uploads/'.$article['pdf']);
            if ($name) {
                return response()->download($name, $file);
            }
        }
    }

    public function downloadRef($id, $type)
    {
        $article = InPressArticle::find($id);
        if (!$article) {
            abort(404, 'can not find article, ID: ' . $id);
        }

        //bibtex|endnote|reference_manager|procite|bibtex|refworks
        
        $typeArr = ['bibtex', 'endnote', 'reference_manager', 'procite', 'refworksbibtex', 'refworks'];

        if (!in_array($type, $typeArr)) {
            abort(404, 'Error type: ' . $type);
        }

        $e = new Export($article, true);
        $method = 'get' . str_replace('_', '', $type);
        if ($type == 'endnote') {
            return Response::make($e->$method(), '200', array('Content-Disposition'=>sprintf('attachment; filename="%s"', 'EndNote_en.enw')))
                ->header('Content-Type', 'application/x-endnote-refer');
        } else {
            return Response::make($e->$method(), '200', array('Content-Disposition'=>sprintf('attachment; filename="%s"', ucwords($type) . '_en.txt')))
                ->header('Content-Type', 'plain/txt');
        }
    }

    public function downloadMultipleRef(Request $request)
    {
        $ids = $request->get('ids', []);
        $type = $request->get('type');
        $typeArr = ['bibtex', 'endnote', 'reference_manager', 'procite', 'refworksbibtex', 'refworks'];
        if (empty($ids) or !in_array($type, $typeArr)) {
            abort(404, 'Error occurred');
        }


        $articles = InPressArticle::find(explode(',', $ids));
        $method = 'get' . str_replace('_', '', $type);
        $string = '';
        foreach ($articles as $article) {
            $e = new Export($article, true);
            $string .= $e->$method();
        }

        if ($type == 'endnote') {
            return Response::make($string, '200', array('Content-Disposition'=>sprintf('attachment; filename="%s"', 'EndNote_en.enw')))
                ->header('Content-Type', 'application/x-endnote-refer');
        } else {
            return Response::make($string, '200', array('Content-Disposition'=>sprintf('attachment; filename="%s"', ucwords($type) . '_en.txt')))
                ->header('Content-Type', 'plain/txt');
        }
    }
}